﻿using Project.Ycm20250401.Service._20250418.Domain.Entities;
using Project.Ycm20250401.Service._20250418.Domain.IRepository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Project.Ycm20250401.Service._20250418.Application
{
    public class OrderService20250422
    {
        //注入订单仓储接口
        private readonly IOrderRepository _orderRepository;

        public OrderService20250422(IOrderRepository orderRepository)
        {   
            _orderRepository = orderRepository;
        }
        /// <summary>
        /// 创建订单
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="orderItems"></param>
        public async Task<bool> CreateOrder(string userId,List<OrderItem> orderItems)
        {
            try
            {
                var order = new Order20250422(userId);
                order.CreateOrder(userId, orderItems);
                await _orderRepository.SaveOrder(order);
                return true;
            }
            catch (Exception ex)
            {
               //抛出下单失败异常,并给出提示，订单创建失败
               throw new Exception("订单创建失败，请稍后重试！");
            }
        }

        //支付订单
        public async Task<bool> PayOrder(string orderId)
        {
            try
            {
                var order = await _orderRepository.GetOrderById(orderId);
                order.Pay(1,"");
                await _orderRepository.PayOrder(orderId);
                return true;
            }
            catch (Exception ex)
            {
                throw new Exception("支付订单失败，请稍后重试！");
            }
         }
    }
}
